package cn.wilson.hrm.web.controller;

import cn.wilson.hrm.utils.AjaxResult;
import cn.wilson.hrm.utils.FastDfsApiOpr;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class FastdfsController {


    /**
     *      上传文件到fastdfs，然后返回文件路径
     * @param file
     * @return
     */
    @RequestMapping("/fastdfs/upload")
    public AjaxResult upload(MultipartFile file){
        try {
            //获取文件名
            String filename = file.getOriginalFilename();
            //获取上传的文件名后缀
            String suffix = FilenameUtils.getExtension(filename);

            //拿到图片路径
            String pathname = FastDfsApiOpr.upload(file.getBytes(), suffix);

            //返回文件路径
            return AjaxResult.me().setResultObj(pathname);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("上传失败");
        }
    }



}
